package softserve.vgolosh.utils;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class CmdUtil {
	private static final int STANDART_TIME_INTERVAL = 500;

	public static void start(String client, String command, String workDir) {
		start(client, command, workDir, STANDART_TIME_INTERVAL);
	}

	public static void start(String client, String command, String workDir,
			int timeInterval) {
		String[] commands = new String[] { "cmd.exe",
				"/c start " + command + "\n" };
		File dir = new File(workDir + client);
		startCommand(commands, dir, timeInterval);
	}

	public static void startCommand(String[] commands, File dir) {
		startCommand(commands, dir, STANDART_TIME_INTERVAL);
	}

	public static void startCommand(String[] commands, File dir,
			int timeInterval) {
		try {
			Runtime.getRuntime().exec(commands, null, dir);
			try {
				Thread.sleep(timeInterval);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}	

	public static void startCommandMap(Map<String, String> commands,
			String working) {
		startCommandMap(commands, working, STANDART_TIME_INTERVAL);
	}

	public static void OpenCmds(String working, List<String> clients) {
		for (String client : clients) {
			start(client, "title " + client, working);
		}
	}

	public static void startCommandMap(Map<String, String> commands,
			String working, int timeInterval) {
		Set<String> clients = commands.keySet();
		for (String client : clients) {
			start(client, commands.get(client), working, timeInterval);
		}
	}

}
